package com.cgbl.cg.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.ClassicCaseAdapter
import com.cgbl.cg.adapter.HomeTabAdapter
import com.cgbl.cg.adapter.MoreRecommendAdapter
import com.cgbl.cg.adapter.ProjectDiscountAdapter
import com.cgbl.cg.databinding.AppFragmentHomeProjectBinding
import com.cgbl.cg.ui.WebActivity
import com.cgbl.cg.vm.home.HomeProjectViewModel
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.model.BannerBean
import com.robot.base.model.ProjectBean
import com.robot.base.model.TypeListBean
import com.robot.base.utils.pic.ImageLoaderUtils
import com.robot.base.view.DividerItemDecoration
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder

class HomeProjectFragment : GLBaseFragment<AppFragmentHomeProjectBinding, HomeProjectViewModel>() {
    private lateinit var bannerTitleAdapter: BannerImageAdapter<BannerBean>

    private lateinit var classicCaseAdapter: ClassicCaseAdapter
    private lateinit var newArrivalAdapter: ProjectDiscountAdapter
    private lateinit var recommendAdapter: ProjectDiscountAdapter
    private lateinit var moreRecommendAdapter: MoreRecommendAdapter
    private lateinit var homeTabAdapter: HomeTabAdapter

    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_home_project

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initBanner()
        initAdapter()
    }

    override fun initData() {
        super.initData()
        viewModel.initData()
        viewModel.refresh()
    }

    private fun initBanner() {
        bannerTitleAdapter = object : BannerImageAdapter<BannerBean>(
            arrayListOf()
        ) {
            override fun onBindView(
                holder: BannerImageHolder?,
                data: BannerBean?,
                position: Int,
                size: Int
            ) {
                ImageLoaderUtils.getGlideManager()
                    .load(data?.imagesFile)
                    .apply(ImageLoaderUtils.getRoundCornerGlideOption(10))
                    .into(holder?.imageView!!)
            }
        }
        binding.banner.setAdapter(bannerTitleAdapter)
        binding.banner.setIndicator(binding.indicator, false)
        binding.banner.setBannerGalleryEffect(12, 4)
        binding.banner.start()

        bannerTitleAdapter.setOnBannerListener { data, position ->
            WebActivity.start(requireContext(), data.hrefUrl ?: "", data?.coverName ?: "")
        }
    }

    private fun initAdapter() {
        classicCaseAdapter = ClassicCaseAdapter()

        binding.recyclerViewClassicCase.apply {
            adapter = classicCaseAdapter
            addItemDecoration(
                DividerItemDecoration(
                    requireContext(),
                    LinearLayoutManager.VERTICAL,
                    0.5f,
                    R.color.app_F7F6FA
                )
            )
        }

        homeTabAdapter = HomeTabAdapter()
        binding.recyclerViewTab.apply {
            adapter = homeTabAdapter
        }
        homeTabAdapter.setOnItemClickListener { adapter, view, position ->
            classicCaseAdapter.setNewInstance(homeTabAdapter.data[position].typeConstsByDateList as MutableList<ProjectBean>?)
        }

        newArrivalAdapter = ProjectDiscountAdapter(arrayListOf())
        binding.bannerNewArrival.setAdapter(newArrivalAdapter)
        binding.bannerNewArrival.setBannerGalleryEffect(84, 0)
        binding.bannerNewArrival.start()

        recommendAdapter = ProjectDiscountAdapter(arrayListOf())
        binding.bannerRecommend.setAdapter(recommendAdapter)
        binding.bannerRecommend.setBannerGalleryEffect(84, 0)
        binding.bannerRecommend.start()

        moreRecommendAdapter = MoreRecommendAdapter()
        binding.recyclerViewMoreRecommend.apply {
            adapter = moreRecommendAdapter
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.homeData.observe(this) {
            newArrivalAdapter.setDatas(it.newCase)
            recommendAdapter.setDatas(it.hotCase)
            moreRecommendAdapter.setNewInstance(it.other as MutableList<ProjectBean>?)
            homeTabAdapter.setNewInstance(it.typeList as MutableList<TypeListBean>?)
        }

        viewModel.uc.ribbonDataEvent.observe(this) {
            bannerTitleAdapter.setDatas(it)
        }


        viewModel.uc.refreshEvent.observe(this) {
            binding.smartRefreshLayout.finishRefresh()
            binding.smartRefreshLayout.finishLoadMore()
        }
        binding.smartRefreshLayout.setOnRefreshListener {
            viewModel.refresh()
        }
//        binding.smartRefreshLayout.setOnLoadMoreListener {
//            it.finishLoadMore()
//            viewModel.toProjectMenuCommand.execute()
//        }
    }
}